package com.bjesc_b2c.app.appraiset.widget;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.bjesc_b2c.app.appraiset.camera.CameraInterface;

/**
 * Created by up_girl on 2017/3/30.
 *
 * 1、注意点：surfaceview变得可见时，surface被创建；surfaceview隐藏，surface被销毁
 * 2、实现过程：继承SurfaceView并实现SurfaceHolder.Callback接口
 * ----> SurfaceView.getHolder()获得SurfaceHolder对象
 * ----> SurfaceHolder.addCallback(callback)添加回调函数
 * ----> SurfaceHolder.lockCanvas()获得Canvas对象并锁定画布
 * ----> Canvas绘画
 * ----> SurfaceHolder.unlockCanvasAndPost(Canvas canvas)结束锁定画图，并提交改变，将图形显示。
 */
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "MySurfaceView";
    private Context mContext;
    private SurfaceHolder mSurfaceHolder;
    private Paint mPaint;
    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mSurfaceHolder = getHolder();
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);//translucent半透明 transparent透明
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder.addCallback(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(false);

}


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //在创建时激发，一般在这里调用画图的线程。
        Log.i(TAG, "surfaceCreated...");

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        //在surface的大小发生改变时激发
        Log.i(TAG, "surfaceChanged...");
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //销毁时激发，一般在这里将画图的线程停止、释放。
        Log.i(TAG, "surfaceDestroyed...");
        CameraInterface.getInstance().doStopCamera();
    }

    public SurfaceHolder getSurfaceHolder() {
        // SurfaceHolder当作surface的控制器，用来操纵surface
        return mSurfaceHolder;
    }



}
